package com.kitty.common.utils;

import com.alibaba.druid.util.Utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

@Service
@Slf4j
public class ToolsPub {
    static ConfigInit configInit = new ConfigInit();



    public static String getIP(HttpServletRequest request)
    {
        String ip = request.getHeader("x-forwarded-for");
        if (!checkIP(ip))
            ip = request.getHeader("Proxy-Client-IP");
        if (!checkIP(ip))
            ip = request.getHeader("WL-Proxy-Client-IP");
        if (!checkIP(ip))
            ip = request.getRemoteAddr();
        return ip;
    }
    private static boolean checkIP(String ip)
    {
        return ip != null && ip.length() != 0 && !"unkown".equalsIgnoreCase(ip) && ip.split(".").length == 4;
    }

    public  void getResFile(String filename) {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
        configInit = JSONObject.parseObject(Utils.read(inputStream), ConfigInit.class);
    }
    public static int getGongMingField(int upgradeLevel,short fieldId,int equipLevel) {
        if (upgradeLevel < 4 || upgradeLevel > 12) {
            return 0;
        }
        equipLevel = (int) (Math.floor(equipLevel / 10) * 10);//取整数
        String[] resonanceField = null;
        if (equipLevel == 70) {
            resonanceField = configInit.getLv_70().get(fieldId+"").split(",");
        } else if (equipLevel == 80) {
            resonanceField = configInit.getLv_80().get(fieldId+"").split(",");
        } else if (equipLevel == 90) {
            resonanceField = configInit.getLv_90().get(fieldId+"").split(",");
        } else if (equipLevel == 100) {
            resonanceField = configInit.getLv_100().get(fieldId+"").split(",");
        } else if (equipLevel == 110) {
            resonanceField = configInit.getLv_110().get(fieldId+"").split(",");
        } else if (equipLevel == 120) {
            resonanceField = configInit.getLv_120().get(fieldId+"").split(",");
        }else if (equipLevel == 130) {
            resonanceField = configInit.getLv_130().get(fieldId+"").split(",");
        }else if (equipLevel == 140) {
            resonanceField = configInit.getLv_140().get(fieldId+"").split(",");
        }else if (equipLevel == 150) {
            resonanceField = configInit.getLv_150().get(fieldId+"").split(",");
        }else if (equipLevel == 160) {
            resonanceField = configInit.getLv_160().get(fieldId+"").split(",");
        }else if (equipLevel == 170) {
            resonanceField = configInit.getLv_170().get(fieldId+"").split(",");
        }
        if (resonanceField != null) {
            try{
                int i = Integer.parseInt(resonanceField[upgradeLevel - 4]);
                return i;
            }catch(Exception e){
                return 0;
            }
        } else {
           log.info("共鸣属性为空,请检查配置!");
           return 0;
        }
    }
}
